import {ElButton, ElTag} from "element-plus";
import {h} from "vue";
import auth from "@/utils/auth";

const render = {

    renderButtonArray: (row, array) => {
        const buttonArray = [];
        array.forEach(item => {
            const hasPower = (item?.powerCodeList === true || item?.powerCodeList === null ? true :
                Array.isArray(item?.powerCodeList) ? auth.hasAllPower(item?.powerCodeList) :
                    auth.hasPower(item?.powerCodeList)) && item?.clickFunction;
            if (hasPower) {
                buttonArray.push(h(ElButton, {
                    type: item?.type,
                    icon: item?.icon,
                    size: item?.size,
                    plain: true,
                    style: {
                        marginRight: '5px'
                    },
                    onClick: () => {
                        if (typeof item?.clickFunction === 'function') {
                            item?.clickFunction(row);
                        }
                    }
                }, item.text));
            }
        });
        return h('span', buttonArray);
    },

    renderOperation: (row, deleteClickFunction, deletePowerCodeList = [], EditClickFunction, editPowerCodeList = []) => {
        const array = [
            {
                type: 'danger',
                icon: 'el-icon-delete',
                size: 'small',
                powerCodeList: deletePowerCodeList,
                clickFunction: deleteClickFunction,
            },
            {
                type: 'warning',
                icon: 'el-icon-edit',
                size: 'small',
                powerCodeList: editPowerCodeList,
                clickFunction: EditClickFunction,
            },
        ];
        return render.renderButtonArray(row, array);
    },

    renderThreeOperation: (row, deleteClickFunction, deletePowerCodeList = [], EditClickFunction, editPowerCodeList = [], contentClickFunction, contentPowerCodeList) => {
        const array = [
            {
                type: 'danger',
                icon: 'el-icon-delete',
                size: 'small',
                powerCodeList: deletePowerCodeList,
                clickFunction: deleteClickFunction,
            },
            {
                type: 'warning',
                icon: 'el-icon-edit',
                size: 'small',
                powerCodeList: editPowerCodeList,
                clickFunction: EditClickFunction,
            },
            {
                type: 'info',
                icon: 'el-icon-DocumentAdd',
                size: 'small',
                powerCodeList: contentPowerCodeList,
                clickFunction: contentClickFunction,
            },
        ];
        return render.renderButtonArray(row, array);
    },

    renderDetailOperation: (row, deleteClickFunction, deletePowerCodeList = [], EditClickFunction, editPowerCodeList = []) => {
        const array = [
            {
                type: 'primary',
                icon: 'el-icon-guide',
                size: 'small',
                powerCodeList: deletePowerCodeList,
                clickFunction: deleteClickFunction,
            },
            {
                type: 'warning',
                icon: 'el-icon-edit',
                size: 'small',
                powerCodeList: editPowerCodeList,
                clickFunction: EditClickFunction,
            },
        ];
        return render.renderButtonArray(row, array);
    },

    renderDict: (type, value) => {
        return h(ElTag, {
            type: type
        }, value);
    },

    renderDictTag: (dictList, value) => {
        const sureDictList = dictList.value.filter(item => item.value === value);
        if (sureDictList.length > 0) {
            return render.renderDict(sureDictList[0].display, sureDictList[0].label);
        } else {
            return h("span", null, '错误');
        }
    },

    renderYesOrNo: (value, trueValue, falseValue) => {
        let isBoolean = false;
        if (typeof value === 'boolean') {
            isBoolean = Boolean(value);
        } else if (typeof value === 'string') {
            switch (value.toLowerCase()) {
                case 'true':
                    isBoolean = true;
                    break;
                case 'Y':
                    isBoolean = true;
                    break;
                case 'false':
                    isBoolean = false;
                    break;
                case 'N':
                    isBoolean = false;
                    break;
                default:
                    isBoolean = false;
                    break;
            }
        }
        return render.renderDict(isBoolean ? 'success' : 'danger', isBoolean ? trueValue : falseValue);
    },

    renderScope: (scope) => {
        return h(ElTag, {
            type: scope === 'ALL' ? 'success' : scope === 'DEPT' ? 'primary' : scope === 'CHILD' ? 'info' : scope === 'CUSTOM' ? 'danger' : 'warning'
        }, scope === 'ALL' ? '全部数据权限' : scope === 'DEPT' ? '本部门数据权限' : scope === 'CHILD' ? '本部门及以下数据权限' : scope === 'CUSTOM' ? '自定义数据权限' : '仅本人数据权限');
    },

    renderUserSex: (sex) => {
        return h(ElTag, {
            type: sex === 0 ? 'primary' : sex === 1 ? 'success' : 'danger'
        }, sex === 0 ? "女" : sex === 1 ? "男" : "未知");
    },

    renderConfigType: (type) => {
        return h(ElTag, {
            type: type === 'SYSTEM_CONFIG' ? 'success' : 'USER_CONFIG' ? 'primary' : 'danger'
        }, type === 'SYSTEM_CONFIG' ? "系统配置" : 'USER_CONFIG' ? '用户配置' : "未知");
    },

    renderInfoType: (type) => {
        return h(ElTag, {
            type: type === 'LOGIN' ? 'success' : 'danger'
        }, type === 'LOGIN' ? "登录" : "登出");
    },

    renderLogType: (type) => {
        return h(ElTag, {
            type: type === 'ADD' ? 'primary' : type === 'DEL' ? 'danger' : type === 'EDIT' ? 'warning' : type === 'QUERY' ? 'success' : 'info'
        }, type === 'ADD' ? "新增" : type === 'DEL' ? "删除" : type === 'EDIT' ? "修改" : type === 'QUERY' ? "查询" : "其他");
    },

    renderTemplateType: (type) => {
        return h(ElTag, {
            type: type === 'Java' ? 'success' : type === 'Vue' ? 'primary' : type === 'JavaScript' ? 'warning' : type === 'Xml' ? 'danger' : type === 'SQL' ? 'info' : 'info'
        }, type);
    },

}

export default render;